﻿Imports System.Data.Linq
Imports System.Linq

Public Class CompanyRepository

    Private dc As New TanNamChinhDataContext

    Public Sub New()
        dc = GeneralClassDAL.dc
    End Sub

    Public Function SelectAllRecord() As IQueryable(Of tblCompany)
        Dim companies = From p In dc.tblCompanies
                        Where p.IsActive = True
                        Select p
        Return companies
    End Function

    Public Function SelectRecordById(ByVal id As Integer) As tblCompany
        Dim company = dc.tblCompanies.SingleOrDefault(Function(p As tblCompany) p.ID_Company = id)
        Return company
    End Function

    Public Function SelectRecordByName(ByVal Name As String) As tblCompany
        Dim query = dc.tblCompanies.SingleOrDefault(Function(p As tblCompany) p.Name = Name And p.IsActive = True)
        Return query
    End Function

    Public Sub Insert(ByVal obj As tblCompany)
        dc.tblCompanies.InsertOnSubmit(obj)
        dc.SubmitChanges()
    End Sub

    Public Sub Update(ByVal obj As tblCompany)
        dc.Refresh(RefreshMode.KeepChanges, obj)
        dc.SubmitChanges(ConflictMode.ContinueOnConflict)
    End Sub

End Class
